# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def hasPathSum(self, root, sum):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: bool
        """
        def traverse(root, acc):
            acc += root.val
            if not (root.left or root.right): return acc==sum
            ret = False
            if root.left: ret = ret or traverse(root.left, acc)
            if root.right: ret = ret or traverse(root.right, acc)
            return ret
        if not root: return False
        else: return traverse(root, 0)